package Dao;

import Entity.Experiment;
import Mybatis.Mybatis;
import org.apache.ibatis.session.SqlSession;
import java.util.List;

public class ExperimentDao implements ExperimentDaoImpl {
    public List<Experiment> findAllExperiment() {
        Mybatis mybatis = new Mybatis();
        SqlSession session = mybatis.getSqlSession();

        String statement = "Mapper.ExperimentMapper.findAllExperiment";
        List<Experiment> experiments = session.selectList(statement);
        session.close();
        return experiments;
    }
    public Experiment findById(int id) {
        Mybatis mybatis = new Mybatis();
        SqlSession session = mybatis.getSqlSession();

        String statement = "Mapper.ExperimentMapper.findById";
        Experiment experiment = session.selectOne(statement, id);
        session.close();
        return experiment;
    }
    public List<Experiment> findByTe_number(String te_number) {
        Mybatis mybatis = new Mybatis();
        SqlSession session = mybatis.getSqlSession();

        String statement = "Mapper.ExperimentMapper.findByTe_number";
        List<Experiment> experiments = session.selectList(statement, te_number);
        session.close();
        return experiments;
    }
    public List<Experiment> findByType(int type) {
        Mybatis mybatis = new Mybatis();
        SqlSession session = mybatis.getSqlSession();

        String statement = "Mapper.ExperimentMapper.findByType";
        List<Experiment> experiments = session.selectList(statement, type);
        session.close();
        return experiments;
    }
    public List<Experiment> findByWeek(int week) {
        Mybatis mybatis = new Mybatis();
        SqlSession session = mybatis.getSqlSession();

        String statement = "Mapper.ExperimentMapper.findByWeek";
        List<Experiment> experiments = session.selectList(statement, week);
        session.close();
        return experiments;
    }
    public boolean addExperiment(Experiment experiment) {
        Mybatis mybatis = new Mybatis();
        SqlSession session = mybatis.getSqlSession();

        String statement = "Mapper.ExperimentMapper.addExperiment";
        int count = session.insert(statement, experiment);
        session.commit();
        session.close();
        if(count == 0) {
            return false;
        } else {
            return true;
        }
    }
    public boolean updateExperiment(Experiment experiment) {
        Mybatis mybatis = new Mybatis();
        SqlSession session = mybatis.getSqlSession();

        String statement = "Mapper.ExperimentMapper.updateExperiment";
        int count = session.update(statement, experiment);
        session.commit();
        session.close();
        if(count == 0) {
            return false;
        } else {
            return true;
        }
    }
    public boolean deleteExperiment(Experiment experiment) {
        Mybatis mybatis = new Mybatis();
        SqlSession session = mybatis.getSqlSession();

        String statement = "Mapper.ExperimentMapper.deleteExperiment";
        int count = session.delete(statement, experiment);
        session.commit();
        session.close();
        if(count == 0) {
            return false;
        } else {
            return true;
        }
    }

    // ����
    public static void main(String[]args) {
        ExperimentDao test = new ExperimentDao();

        Experiment experiment = new Experiment("ʵ��9", "1234", "��ʦһ", 1, 1, 20);
        experiment.setTeURL("haha");
        test.addExperiment(experiment);
        experiment.setName("ʵ���");
        experiment.setId(5);
        test.updateExperiment(experiment);
        test.deleteExperiment(experiment);
        List<Experiment> experiments = test.findAllExperiment();
        for(int i = 0; i < experiments.size(); i++) {
            System.out.println(experiments.get(i).getId()+" "+experiments.get(i).getName()+" "+
                    experiments.get(i).getTe_number()+" "+experiments.get(i).getTe_name());
        }

        /*
        Experiment experiment = test.findById(3);
        System.out.println(experiment.getId()+" "+experiment.getName()+" "+
                experiment.getTe_number()+" "+experiment.getTe_name());
         */
        /*
        List<Experiment> experiments = test.findByTe_number("5678");
        for(int i = 0; i < experiments.size(); i++) {
            System.out.println(experiments.get(i).getId()+" "+experiments.get(i).getName()+" "+
                    experiments.get(i).getTe_number()+" "+experiments.get(i).getTe_name());
        }
         */
        /*
        List<Experiment> experiments = test.findByType(2);
        for(int i = 0; i < experiments.size(); i++) {
            System.out.println(experiments.get(i).getId()+" "+experiments.get(i).getName()+" "+
                    experiments.get(i).getTe_number()+" "+experiments.get(i).getTe_name());
        }
         */
        /*
        List<Experiment> experiments = test.findByWeek(12);
        for(int i = 0; i < experiments.size(); i++) {
            System.out.println(experiments.get(i).getId()+" "+experiments.get(i).getName()+" "+
                    experiments.get(i).getTe_number()+" "+experiments.get(i).getTe_name());
        }
         */
    }
}
